import java.util.Arrays;

public class CountPaths {
    public static void main(String[] args) {
        CountPaths countPaths = new CountPaths();
        int[][] nums = {{12469, 18741, 68716, 30594, 65029, 44019, 92944, 84784, 92781, 5655, 43120, 81333, 54113, 88220, 23446, 6129, 2904, 48677, 20506, 79604, 82841, 3938, 46511, 60870, 10825, 31759, 78612, 19776, 43160, 86915, 74498, 38366, 28228, 23687, 40729, 42613, 61154, 22726, 51028, 45603, 53586, 44657, 97573, 61067, 27187, 4619, 6135, 24668, 69634, 24564, 30255, 51939, 67573, 87012, 4106, 76312, 28737, 7704, 35798}};
        System.out.println(countPaths.countPaths(nums));
    }

    boolean[][] flag;
    long count;
    static final int MOD = (int) (1e9 + 7);
    int[][] temp = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    public int countPaths(int[][] grid) {
        long ans = 0;
        int m = grid.length;
        int n = grid[0].length;
        flag = new boolean[m][n];
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            Arrays.fill(dp[i], -1);
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ans += dfs1(grid, i, j, m, n, dp);
            }
        }
        return (int) (ans % MOD);
    }

    public void dfs(int[][] grid, int i, int j, int m, int n, int[][] dp) {
        if (dp[i][j] != Integer.MIN_VALUE) {
            count += dp[i][j];
            return;
        }
        long pre = count;
        ++count;
        flag[i][j] = true;
        for (int[] ints : temp) {
            int x = ints[0] + i;
            int y = ints[1] + j;
            if (x >= 0 && x < m && y >= 0 && y < n && !flag[x][y] && grid[x][y] > grid[i][j]) {
                dfs(grid, x, y, m, n, dp);
            }
        }
        flag[i][j] = false;
        dp[i][j] = (int) ((count - pre) % MOD);
    }

    public int dfs1(int[][] grid, int i, int j, int m, int n, int[][] dp) {
        if (dp[i][j] != -1) return dp[i][j];
        long ans = 1;
        for (int[] ints : temp) {
            int x = ints[0] + i;
            int y = ints[1] + j;
            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] < grid[i][j]) {
                ans += dfs1(grid, x, y, m, n, dp);
            }
        }
        dp[i][j] = (int) (ans % MOD);
        return (int) (ans % MOD);
    }
}
